//we have to include the libary 
#include "LedControl.h"
#include "EEPROM.h"

#define SET_OPERATION 0
#define SET_CURRENT_LIMIT_LOW 1
#define SET_CURRENT_LIMIT_HIGH 2

// create the LedControl
LedControl lc=LedControl(9,11,10,1);
// select the input pin for the potentiometer
int sensorPin = A0;
// constants for buttons
const int button_SET = 2;
const int button_UP = 3;
const int button_DOWN = 4;
// relay pin
const int relayPin = 12;
// current limit variables
byte current_limit_LOW = 0;
byte current_limit_HIGH = 0;
// variable for reading the pushbutton status
byte setButtonState = 0;
// variables for current calculation
int sensorValue = 0;
float current = 0;
byte current_multiplied = 0;
byte current_ones = 0;
byte current_tens = 0;
byte current_hundreds = 0;
byte current_limit_ones_high = 0;
byte current_limit_tens_high = 0;
byte current_limit_hundreds_high = 0;
byte current_limit_ones_low = 0;
byte current_limit_tens_low = 0;
byte current_limit_hundreds_low = 0;

boolean first_start = true;


void setup() {
  // wake up the MAX72XX from power-saving mode 
  lc.shutdown(0,false);
  // set a medium brightness for the Leds
  lc.setIntensity(0,8);
  // clear the display;
  lc.clearDisplay(0);
  // initialize the relay pin as an output.
  pinMode(relayPin, OUTPUT);
  // set relay OFF
  digitalWrite(relayPin, LOW);
  //read current_limit_LOW value from EEPROM address 0
  current_limit_LOW = EEPROM.read(0);
  //read current_limit_HIGH value from EEPROM address 0
  current_limit_HIGH = EEPROM.read(1);
}

void loop() {

//  EEPROM.write(0, 10);
//  EEPROM.write(1, 70);

  // current limit setting
  if(digitalRead(button_SET) && setButtonState == SET_OPERATION) {
    setButtonState = SET_CURRENT_LIMIT_LOW;
    // wait for button release
    while(digitalRead(button_SET)) {};
  }else if(digitalRead(button_SET) && setButtonState == SET_CURRENT_LIMIT_LOW){
    setButtonState = SET_CURRENT_LIMIT_HIGH;
    // wait for button release
    while(digitalRead(button_SET)) {};
  }else if(digitalRead(button_SET) && setButtonState == SET_CURRENT_LIMIT_HIGH){
    setButtonState = SET_OPERATION;  
    // wait for button release
    while(digitalRead(button_SET)) {};
    EEPROM.write(0, current_limit_LOW);
    EEPROM.write(1, current_limit_HIGH);
  }

  switch(setButtonState) {
    case SET_OPERATION:
      // read the value from the sensor
      sensorValue = analogRead(sensorPin);
      // convert the analog reading (which goes from 0 - 1023) to a current (0 - 20A)
      // multiplie current by 10 for splitting to digits and convert to byte value
      current = sensorValue * (5.0 / 1023.0) * 100;
      // convert float value to byte value
      current_multiplied = (byte)current;
      // get ones ...
      current_ones = current_multiplied % 10;
      // get tens
      current_tens = (current_multiplied / 10) % 10;
      // get hundreds
      current_hundreds = ((current_multiplied / 100) % 10);
      
      // check if current is less than 10A
      if(current < 100) {
        // write number before the decimal point on first digit (with decimal point)
        lc.setChar(0, 0, current_tens, true);
        // write number after the decimal point on first digit (without decimal point)
        lc.setChar(0, 1, current_ones, false);
      }
      // check if current is more than 25A
      else if (current > 250) {
        // write '-' on first digit (without decimal point)
        lc.setChar(0, 0, '-', false);
        // write '-' on second digit (without decimal point)
        lc.setChar(0, 1, '-', false);
      }
      // check if current is more than 10A and less then 25A
      else {
        // write tens (hundreds) on first digit (without decimal point)
        lc.setChar(0, 0, current_hundreds, false);
        // write ones (tens) on second digit (without decimal point)
        lc.setChar(0, 1, current_tens, false);
      
      }
      // delay for display stability
      delay(500);

      // check current value and take action if needed
      // at first start let it run
      if(first_start == true) {
          digitalWrite(relayPin, LOW);
          // if the operating current is reached
          if(current > current_limit_LOW && current < current_limit_HIGH)
            first_start = false;
      }else {
         if(current > current_limit_LOW && current < current_limit_HIGH)
           digitalWrite(relayPin, LOW);
         else {
           digitalWrite(relayPin, HIGH);
           delay(500);
           asm volatile ("  jmp 0");
         }
      }
      break;

    case SET_CURRENT_LIMIT_LOW:
      // if UP button is pressed increment voltage value
      if(digitalRead(button_UP) && current_limit_LOW < 200) {
        if(current_limit_LOW >= 100){
          current_limit_LOW = current_limit_LOW + 10;      
        }else {
          current_limit_LOW++;            
        }
        // wait for button release
        while(digitalRead(button_UP)) {};
      }
      // if DOWN button is pressed decrement voltage value
      if(digitalRead(button_DOWN) && current_limit_LOW > 0) {
        if(current_limit_LOW > 100){
          current_limit_LOW = current_limit_LOW - 10;      
        }else {
          current_limit_LOW--;
        }
        // wait for button release
        while(digitalRead(button_DOWN)) {};
      }
      // get ones for LOW 
      current_limit_ones_low = current_limit_LOW % 10;
      // get tens for LOW 
      current_limit_tens_low = (current_limit_LOW / 10) % 10;
      // get hundreds for LOW 
      current_limit_hundreds_low = ((current_limit_LOW / 100) % 10);

      if(current_limit_hundreds_low < 1) {
        lc.setDigit(0, 0, current_limit_tens_low, true);
        lc.setDigit(0, 1, current_limit_ones_low, false);      
      }else {
        lc.setDigit(0, 0, current_limit_hundreds_low, false);
        lc.setDigit(0, 1, current_limit_tens_low, false);          
      }
      break;

    case SET_CURRENT_LIMIT_HIGH:
      // if UP button is pressed increment voltage value
      if(digitalRead(button_UP) && current_limit_HIGH < 200) {
        if(current_limit_HIGH >= 100){
          current_limit_HIGH = current_limit_HIGH + 10;      
        }else {
          current_limit_HIGH++;            
        }
        // wait for button release
        while(digitalRead(button_UP)) {};
      }
      // if DOWN button is pressed decrement voltage value
      if(digitalRead(button_DOWN) && current_limit_HIGH > 0) {
        if(current_limit_HIGH > 100){
          current_limit_HIGH = current_limit_HIGH - 10;      
        }else {
          current_limit_HIGH--;
        }
        // wait for button release
        while(digitalRead(button_DOWN)) {};
      }
      // get ones for HIGH
      current_limit_ones_high = current_limit_HIGH % 10;
      // get tens for HIGH 
      current_limit_tens_high = (current_limit_HIGH / 10) % 10;
      // get hundreds for HIGH 
      current_limit_hundreds_high = ((current_limit_HIGH / 100) % 10);

      if(current_limit_hundreds_high < 1) {
        lc.setDigit(0, 0, current_limit_tens_high, true);
        lc.setDigit(0, 1, current_limit_ones_high, false);      
      }else {
        lc.setDigit(0, 0, current_limit_hundreds_high, false);
        lc.setDigit(0, 1, current_limit_tens_high, false);          
      }
      break;
  }
}

